Overview
process_market_breadth.py is a Phase 4 analytics script that calculates Relative Strength (RS) scores for all stocks and generates comprehensive sector and industry-level breadth metrics for the Market Breadth Dashboard.
Pipeline Position: Phase 4 - Analytics generationCritical Function: Powers the multi-tab Market Breadth Dashboard with Moving Average, RS Rating, and Near-High analytics
Purpose
This script performs two major functions:- RS Score Calculation: Computes weighted Relative Strength ratings (1-99) for all stocks
- Breadth Analytics: Generates sector and industry-level breadth metrics across three dimensions:
- Moving Average breadth (SMA 200/50/20)
- RS Rating distribution (RS 70/80/90)
- Near 52-Week High distribution (1%/2%/5%)
Input Files
Master stock database with price returns and technical indicators including:
1 Year Returns(%),6 Month Returns(%),3 Month Returns(%),1 Month Returns(%)SMA Status(e.g., “Above SMA 200, Above SMA 50”)% from 52W HighSectorandBasic Industryclassifications
Output Files
Updated master file with new
RS Rating field added to each stock (1-99 scale)Comprehensive sector and industry breadth metrics for dashboard renderingStructure:
Processing Logic
1. RS Score Calculation
Calculates a weighted Relative Strength score using multiple timeframes:- 1 Year Returns: 40%
- 6 Month Returns: 20%
- 3 Month Returns: 20%
- 1 Month Returns: 20%
2. Breadth Flags Calculation
Pre-calculates boolean flags for efficient aggregation:3. Sector-Level Analytics
Generates breadth percentages for each sector:4. Industry-Level Analytics
Generates industry metrics with sector contribution calculations:Fields Added to Master Stock Database
Relative Strength rating on a 1-99 scale
- Higher values indicate stronger relative performance
- Calculated using weighted multi-timeframe returns
- Percentile-based ranking across all stocks
Sector Analytics Fields
Percentage of stocks in sector trading above their 200-day SMA
Percentage of stocks in sector trading above their 50-day SMA
Percentage of stocks in sector trading above their 20-day SMA
Percentage of stocks in sector with RS Rating >= 70
Percentage of stocks in sector with RS Rating >= 80
Percentage of stocks in sector with RS Rating >= 90
Percentage of stocks within 1% of 52-week high
Percentage of stocks within 2% of 52-week high
Percentage of stocks within 5% of 52-week high
Industry Contribution Metrics
(Industry stocks above SMA 50) / (Total sector stocks) * 100Measures the industry’s absolute contribution to sector strength
(Industry stocks with RS >= 80) / (Total sector stocks) * 100
(Industry stocks within 5% of high) / (Total sector stocks) * 100
Usage Example
Dashboard Integration
Thesector_analytics.json output powers three dashboard tabs:
- Moving Average Tab: Uses
Breadth_SMA200/50/20metrics - RS Rating Tab: Uses
Breadth_RS70/80/90metrics - Near High Tab: Uses
NearHigh_1pc/2pc/5pcmetrics
Performance Notes
- Market cap filter (300 Cr minimum) is currently disabled per user request
- Temporary calculation columns are dropped before saving to reduce file size
- All breadth percentages are rounded to 1 decimal place
Related Scripts
- bulk_market_analyzer.py - Provides input data with returns and technical indicators
- process_historical_market_breadth.py - Generates time-series breadth data